package nuc.soft.xsf.controller;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import nuc.soft.xsf.interceptors.GlobalConstants;
import nuc.soft.xsf.model.User;
import nuc.soft.xsf.model.UserPaper;
import nuc.soft.xsf.service.IUserPaperService;
import nuc.soft.xsf.service.IUserService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/UserController")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private IUserPaperService userPaperService;
    
    @RequestMapping(value="/add.action" ,produces="text/html;charset=UTF-8")
    @ResponseBody
    public  String addUser(User user){
    	user.setDeletestate(0);
    	userService.insert(user);
    	JSONObject json=new JSONObject();
    	json.put("data", user);
		return json.toString();
    }

    
    @RequestMapping(value="/register.action" ,produces="text/html;charset=UTF-8")
    public  String registerUser(User user,MultipartFile photofile,HttpServletRequest request){
    	
    		user.setDeletestate(0);
        	String userImage=userService.uploadFile(photofile, request);
        	user.setUserImage(userImage);
        	user.setUserState(2+"");
        	userService.insert(user);

        return "redirect:/pages/login.jsp";  
    }
    
    
    
    
    @RequestMapping(value="/delete.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String deleteUser(String ids){
    	String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			User user = userService.findById(Integer.parseInt(strTmp));
			if (user != null) {
				user.setDeletestate(1);
				userService.update(user);
				success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}
    	
    
 
    @RequestMapping(value="/update.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String updateUser(User user){
    	userService.update(user);
    	JSONObject json=new JSONObject();
    	User user1=userService.findById(user.getUserId());
    	json.put("data", user1);
    	json.put("redata",user.getUserName());
    	json.put("error_code", 200);
    	return json.toString();
    }
    
    @RequestMapping(value="/update01.action",produces="text/html;charset=utf-8")
    public String updateUser01(User user,MultipartFile photofile,HttpServletRequest request){
    	String userImage=userService.uploadFile(photofile, request);
    	user.setUserImage(userImage);
    	userService.update(user);
    	JSONObject json=new JSONObject();
    	User user1=userService.findById(user.getUserId());
    	json.put("data", user1);
    	json.put("redata",user.getUserName());
    	json.put("error_code", 200);
    	return "redirect:personInformation.action";
    }
    
    
    @RequestMapping(value="/findByCondition.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String findByCondition(User user){
        Map map=Tools.transBean2Map(user);
        List<User> list=userService.findByCondition(map);
    	JSONObject json=new JSONObject();
    	json.put("record",list);
    	return json.getString("record");
    	
    }
	
    @RequestMapping(value="/JudgeUser.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String JudgeUser(User user){   	
        Map map=Tools.transBean2Map(user);
        List<User> list=userService.findByCondition(map);
    	JSONObject json=new JSONObject();
        if(list.size()>0){
        	json.put("error_code", 500);
        }
        else{
        	json.put("error_code", 200);
        }
    	
    	return json.toString();
    	
    }

    @RequestMapping(value="/login.action",produces="text/html;charset=utf-8")
    public String findByCondition02(HttpServletRequest request,HttpServletResponse Response,User user){
        Map map=Tools.transBean2Map(user);
        List<User> list=userService.findByCondition(map);   
    	if(list.size()>0){
    		 User user01=list.get(0);
    		 System.out.println("user01 image:"+user01.getUserImage());
    		 request.getSession().setAttribute("UserId",list.get(0).getUserId() );
    		 request.getSession().setAttribute("User", user01);
    		 request.getSession().setAttribute(GlobalConstants.Login_UserName,
    				 user01);
    		 UserPaper userPaper=new UserPaper();
    		 userPaper.setUserId(list.get(0).getUserId());
    		 userPaper.setState(1);
    		 Map map01=Tools.transBean2Map(userPaper);
    		 List<UserPaper> listUserPaper= userPaperService.findByCondition(map01);
    		 if(listUserPaper.size()>0){
    			 request.getSession().setAttribute("message", "您的报告已被公司查阅");
    		 }
    		 else{
    		     request.getSession().setAttribute("message", "还没有公司查看过您的报告");
    		 }
    		 
    		 return "redirect:/pages/index.jsp";  
    	}
    	else{
    		return "redirect:/pages/login.jsp";  
    	}
    }
    @RequestMapping(value="/loginback.action",produces="text/html;charset=utf-8")
    public String findByCondition03(HttpServletRequest request,HttpServletResponse Response,User user){
	    	Map map=Tools.transBean2Map(user);
	        List<User> list=userService.findByCondition(map);
	    	if(list.size()>0&&!list.get(0).getUserState().equals("2")){
	    		 User user01=list.get(0);
	    		 request.getSession().setAttribute("User01", user01);
	    		 request.getSession().setAttribute(GlobalConstants.LOGIN_USER,
	    				 user01);
	    		 System.out.println(request.getSession().getAttribute("GlobalConstants.LOGIN_USER"));
	    		 return "redirect:/index.jsp";  
	    	}
	    	else{
	    		
	    		return "redirect:/pages/loginback.jsp";  
	    	}
    }
	
    @RequestMapping(value="/loginbackValidate.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String findByCondition03Validate(User user,HttpServletRequest request,HttpServletResponse Response){  
	    	Map map=Tools.transBean2Map(user);
	        List<User> list=userService.findByCondition(map);
	        JSONObject json=new JSONObject();
	    	if(list.size()>0&&!list.get(0).getUserState().equals("2")){
	    		 json.put("error_code", 100);
	    		 request.getSession().setAttribute("UserId",list.get(0).getUserId() );
	    		 request.getSession().setAttribute(GlobalConstants.LOGIN_USER,
	    				 list.get(0));
	    	}
	    	else{
	    		
	    		 json.put("error_code", 200);
	    	}
	    	return json.toString();
    }
    
	@RequestMapping(value = "/userLogout01.action", method = RequestMethod.GET)
	public String logout(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession();
			session.removeAttribute("User01");
			session.removeAttribute(GlobalConstants.LOGIN_USER);
			session.invalidate();
			return "redirect:/pages/loginback.jsp";
		} catch (Exception e) {
			return "error";
		}

	}
    
	
	@RequestMapping(value = "/userLogout.action", method = RequestMethod.GET)
	public String logoutfront(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession();
			request.removeAttribute("UserId");
		    request.removeAttribute("User");
			session.invalidate();
			return "redirect:/pages/login.jsp";
		} catch (Exception e) {
			return "error";
		}
	}
    
    //返回个人信息的对象
    @RequestMapping(value="/personInformation.action",produces="text/html;charset=utf-8")
    public String getUserInformation(HttpServletRequest request){
    	int userId= (int) request.getSession().getAttribute("UserId");
    	User u=userService.findById(userId);
    	request.getSession().setAttribute("userInfo", u);
    	return "redirect:/pages/personInfo.jsp";
    }
}
